package com.ryujung.binary_tree.leetCode_513;

import java.util.LinkedList;
import java.util.Queue;


class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    // 类似二叉树的层序遍历，
    // 每次在进行某层遍历时，将最左侧的值记录下来，
    // 最后返回即可
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> que = new LinkedList<>();
        que.add(root);
        TreeNode leftNode = null;
        while (!que.isEmpty()) {
            int size = que.size();
            leftNode = que.peek();
            for (int i = 0; i < size; i++) {
                TreeNode node = que.poll();
                if (node.left != null) {
                    que.add(node.left);
                }
                if (node.right != null) {
                    que.add(node.right);
                }
            }
        }
        return leftNode.val;
    }
}